package cn.tedu.knows.sys.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/v1/home")
public class HomeController {

    //Spring Security框架提供了判断是否拥有权限或角色的类型
    public static final GrantedAuthority STUDENT=
            new SimpleGrantedAuthority("ROLE_STUDENT");
    public static final GrantedAuthority TEACHER=
            new SimpleGrantedAuthority("ROLE_TEACHER");

    @GetMapping
    public String index(
            @AuthenticationPrincipal UserDetails user){

        //如果是老师
        if(user.getAuthorities().contains(TEACHER)){
            return "/index_teacher.html";
        }else if(user.getAuthorities().contains(STUDENT)){
            //如果是学生
            return "/index_student.html";
        }
        // 不是老师也不是学生
        return "/login.html";
    }
}
